package org.pinda.gui;

import java.util.ArrayList;


public class Gui extends GuiComponent {
	
	/**
	 * The components on this Gui
	 */
	private ArrayList<GuiComponent> components;
	/**
	 * Requests the game to close
	 */
	private boolean gameClose;
	
	public Gui(Gui parent, int flags) {
		this(parent, flags, 0, 0, parent.width, parent.height);
	}
	
	public Gui(Gui parent, int flags, float x, float y, float width, float height) {
		super(parent, flags);
		this.parent = parent;
		this.gameClose = false;
		this.components = new ArrayList<>();
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	/**
	 * Requests a close of the entire game<br/>
	 * This function will cascade up to all Gui's
	 */
	public void requestClose() {
		if(parent != null)
			parent.requestClose();
		gameClose = true;
	}
	
	/**
	 * Removes itself from the parent and adds the given Gui to the parent
	 * @param newGui The Gui to display
	 * @param cleanResources If the old gui resources can be released
	 */
	public void switchGui(Gui newGui, boolean cleanResources) {
		parent.removeComponent(this);
		if(cleanResources) {
			onDelete();
			if(components != null) {
				for(GuiComponent guiComp : components) {
					guiComp.onDelete();
				}
			}
		}
		parent.addComponent(newGui);
	}
	
	/**
	 * Removes itself from the parent and adds the given Gui to the parent<br/>
	 * This function will also release the resources from the old gui
	 * @param newGui The Gui to display
	 */
	public void switchGui(Gui newGui) {
		switchGui(newGui, true);
	}
	
	/**
	 * Adds a component to the Gui
	 * @param component The component to be added
	 */
	public void addComponent(GuiComponent component) {
		components.add(component);
	}
	
	/**
	 * Removes a component from the Gui
	 * @param component The component to be removed
	 */
	public void removeComponent(GuiComponent component) {
		components.remove(component);
	}
	
	@Override
	public void setFps(int fps) {
		for(GuiComponent component : components) {
			component.setFps(fps);
		}
	}
	
	@Override
	public void onClick(int mouseButton, int x, int y) {
		for(GuiComponent component : components) {
			component.removeFocus();
			if(x >= component.getX() && x <= (component.getX() + component.getWidth()) && y >= component.getY() && y <= (component.getY() + component.getHeight())) {
				component.onClick(mouseButton, x, y);
			}
		}
	}
	
	@Override
	public void onKeyPress(char key) {
		for(GuiComponent component : components) {
			component.onKeyPress(key);
		}
	}
	
	@Override
	public void onTick(int deltaMs) {
		for(GuiComponent component : components) {
			component.onTick(deltaMs);
		}
	}

	@Override
	public boolean canDelete() {
		return false;
	}
	
	@Override
	public void render() {
		super.render();
		for(GuiComponent component : components) {
			component.render();
		}
	}
	
	/**
	 * Checks if a close has been requested
	 * @return true on closeRequest else false
	 */
	public boolean isCloseRequested() {
		return gameClose;
	}

}
